<?php
/**
 * Name: 角色管理.
 *
 * Author:李强 EdwinLee
 * Company:上海煦鸣网络科技有限公司
 * Time: 2018-01-10 21:25
 */

namespace App\Services\Repo;

use App\Models\Role;
use App\Services\Interfaces\RepoServiceInterface;
use App\Services\Service\Helper;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoleRepo implements RepoServiceInterface
{

    /**
     * 查询数据集合
     * @param array $where 条件
     */
    public function index(Request $request)
    {
        $query_fields = ['page' => 1, 'limit' => 10, 'opt_type' => '', 'keywords' => ''];
        $inputs = Helper::getInputs($request->all(), $query_fields);
        $page = intval($inputs['page']);
        $limit = intval($inputs['limit']);
        $start = ($page - 1) * $limit;
        $where = [];
        if ($inputs['opt_type'] != "" && $inputs['keywords'] != "") {
            $where['where'][] = [$inputs['opt_type'], 'like', '%' . $inputs['keywords'] . '%'];
        }
        $result = Role::queryAll($where, [], [$start, $limit]);
        $result['conditions'] = $inputs;
        return $result;
    }

    /**
     * 新增数据
     * @param array|null $_options //数据参数
     * @return Model
     */
    public function store(array $_options = null): array
    {
        $result = ['code' => 0, 'message' => '保存失败'];
        $role = new Role();
        $role->name = $_options['name'];  //权限名称
        $role->display_name = $_options['display_name'];    //权限显示名称
        $role->description = $_options['description'];  //权限描述
        if (false !== $role->save()) {
            $result['code'] = 1;
            $result['message'] = '保存成功';
        }
        return $result;
    }

    /**
     * 查询数据详情
     * @param array $_filter 过滤条件
     * @return Model
     */
    public function show(array $_filter): Model
    {
        $result = Role::find($_filter)->first();
        if ($result) {
            return $result;
        }
    }

    /**
     * 修改数据
     * @param array $_filter 过滤条件
     * @param array $_update_data 要修改的数据
     * @return bool
     */
    public function update(array $_filter, array $_update_data): array
    {
        $result = ['code' => 0, 'message' => '更新失败'];
        $role = Role::find($_filter['id']);
        if (!is_null($role)) {
            $role->name = $_update_data['name'];  //权限名称
            $role->display_name = $_update_data['display_name'];    //权限显示名称
            $role->description = $_update_data['description'];  //权限描述
            if (false !== $role->save()) {
                $result['code'] = 1;
                $result['message'] = '保存成功';
            }
        } else {
            $result['message'] = '查询角色失败';
        }
        return $result;
    }

    /**
     * 删除数据
     * @param array $_filter 过滤条件
     * @return bool
     */
    public function destroy(array $_filter): array
    {
        $result = ['code' => 0, 'message' => '删除失败'];
        $role = Role::where($_filter)->first();
        if (is_null($role)) {
            $result['message'] = '角色不存在';
        } else {
            if (false !== $role->delete()) {
                //删除管理员角色
                DB::table('role_admin')->where('role_id', $_filter['id'])->delete();
                DB::table('permission_role')->where('role_id', $_filter['id'])->delete();
                $result['code'] = 1;
                $result['message'] = '删除成功';
            }
        }
        return $result;
    }
}